package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.base.BaseYMDTimeDialogFragment;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.Date;

/**
 * 知识公告查询
 *
 * @author wu
 */
public class KnowLedgeAnnounceSearchFragment extends Fragment implements OnClickListener {
  public static final String EXTRA_START_DATE = "com.isunland.managesystem.ui.EXTRA_START_DATE";
  public static final String EXTRA_END_DATE = "com.isunland.managesystem.ui.EXTRA_END_DATE";
  public static final String EXTRA_TYPE_NAME = "com.isunland.managesystem.ui.EXTRA_TYPE_NAME";
  public static final String EXTRA_TYPE_CODE = "com.isunland.managesystem.ui.EXTRA_TYPE_CODE";
  public static final String EXTRA_KEY_WORDS = "com.isunland.managesystem.ui.EXTRA_KEY_WORDS";

  private static final int REQUEST_STARTTIME = 0;
  private static final int REQUEST_FINISHTIME = 1;
  private static final int REQUEST_TYPE = 2;

  private TextView mtvBeginDateKnowledgeAnnounce;// 开始时间
  private TextView mtvEndDateKnowledgeAnnounce;// 结束时间
  private TextView mtvTypeKnowledgeAnnounce;// 公文类别
  private EditText metKeyWordsKnowledgeAnnounce;// 关键字

  private String mStartTime;
  private String mEndTime;
  private String mTypeName;
  private String mTypeCode;
  private String mKeyWords;

  @Override public void onCreate(Bundle savedInstanceState) {
    // 设置返回图标
    setHasOptionsMenu(true);

    BaseVolleyActivity activity = (BaseVolleyActivity) getActivity();
    ActionBar actionBar;
    if (activity != null) {
      actionBar = activity.getSupportActionBar();
      if (actionBar != null) {
        if (NavUtils.getParentActivityName(activity) != null) {
          actionBar.setDisplayHomeAsUpEnabled(true);
        }
        actionBar.setTitle(R.string.knowledgeAnnounce);
      }
    }
    mStartTime = getArguments().getString(EXTRA_START_DATE);
    mEndTime = getArguments().getString(EXTRA_END_DATE);
    mTypeName = getArguments().getString(EXTRA_TYPE_NAME);
    mTypeCode = getArguments().getString(EXTRA_TYPE_CODE);
    mKeyWords = getArguments().getString(EXTRA_KEY_WORDS);
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_knowledge_announce, container, false);
    mtvBeginDateKnowledgeAnnounce =
        (TextView) view.findViewById(R.id.tv_beginDate_knowledgeAnnounce);
    mtvEndDateKnowledgeAnnounce = (TextView) view.findViewById(R.id.tv_endDate_knowledgeAnnounce);
    mtvTypeKnowledgeAnnounce = (TextView) view.findViewById(R.id.tv_Type_knowledgeAnnounce);
    metKeyWordsKnowledgeAnnounce = (EditText) view.findViewById(R.id.tv_keyWords_knowledgeAnnounce);

    mtvBeginDateKnowledgeAnnounce.setText(mStartTime);
    mtvEndDateKnowledgeAnnounce.setText(mEndTime);
    mtvTypeKnowledgeAnnounce.setText(mTypeName);
    metKeyWordsKnowledgeAnnounce.setText(mKeyWords);

    mtvBeginDateKnowledgeAnnounce.setOnClickListener(this);
    mtvEndDateKnowledgeAnnounce.setOnClickListener(this);
    mtvTypeKnowledgeAnnounce.setOnClickListener(this);
    return view;
  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      case R.id.tv_beginDate_knowledgeAnnounce:
        showDialog(REQUEST_STARTTIME);
        break;
      case R.id.tv_endDate_knowledgeAnnounce:
        showDialog(REQUEST_FINISHTIME);
        break;
      case R.id.tv_Type_knowledgeAnnounce:
        showDialog(REQUEST_TYPE);
        break;

      default:
        break;
    }
  }

  private void showDialog(int requestCode) {
    DialogFragment dialog = null;

    switch (requestCode) {
      case REQUEST_STARTTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_FINISHTIME:
        dialog = new BaseYMDTimeDialogFragment();
        break;
      case REQUEST_TYPE:
        //公文类别
        dialog = PublishKnowledgeTypeDialogFragment.newInstance(
            PublishKnowledgeTypeDialogFragment.KNOWLEDGE);
        break;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(KnowLedgeAnnounceSearchFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_STARTTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mStartTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mtvBeginDateKnowledgeAnnounce.setText(mStartTime);
    }

    if (requestCode == REQUEST_FINISHTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseYMDTimeDialogFragment.EXTRA_DATE);
      mEndTime = MyDateUtil.dateToString(acquireDate, MyDateUtil.TYPE_DATE);
      mtvEndDateKnowledgeAnnounce.setText(mEndTime);
      return;
    }
    if (requestCode == REQUEST_TYPE && data != null) {
      CustomerDialog dialog =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      mTypeName = dialog.getName();
      mTypeCode = dialog.getId();
      mtvTypeKnowledgeAnnounce.setText(mTypeName);
      return;
    }

    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:

        if (!MyDateUtil.startBeforeEnd(mStartTime, mEndTime)) {
          ToastUtil.show(R.string.startTimeBeforeEndTime);
        } else {
          Intent intent = new Intent();
          intent.putExtra(EXTRA_START_DATE, mStartTime);
          intent.putExtra(EXTRA_END_DATE, mEndTime);
          intent.putExtra(EXTRA_TYPE_NAME, mTypeName);
          intent.putExtra(EXTRA_TYPE_CODE, mTypeCode);
          intent.putExtra(EXTRA_KEY_WORDS, metKeyWordsKnowledgeAnnounce.getText().toString());
          getActivity().setResult(Activity.RESULT_OK, intent);
          getActivity().finish();
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  public static Fragment newInstance(String startDate, String endDate, String typeName,
      String typeCode, String keyWords) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_START_DATE, startDate);
    bundle.putString(EXTRA_END_DATE, endDate);
    bundle.putString(EXTRA_TYPE_NAME, typeName);
    bundle.putString(EXTRA_TYPE_CODE, typeCode);
    bundle.putString(EXTRA_KEY_WORDS, keyWords);
    KnowLedgeAnnounceSearchFragment fragment = new KnowLedgeAnnounceSearchFragment();
    fragment.setArguments(bundle);
    return fragment;
  }
}
